<template>
	<view class="logistics">
		<view class="pack" v-if="ordergoods.length>1">
			<view class="packTit">您的订单被分成多个包裹：</view>
			<view class="packList">
				<view :class="['packOne',selGoodId == item.id?'active':'']" v-for='(item,index) in ordergoods' :key='index' @tap='onchangeShop(item)'>
					<view class="packInfo">
						<view class="oneTit">包裹{{index + 1}}</view>
						<view class="packCount">
							<text>共{{item.goods_num}}件</text>
							<view>{{item.storage_status == '1'?'已发货':'未发货'}}</view>
						</view>
					</view>
					<view class="packImg imgPublic">
						<image :src="item.goods.main_img" mode="widthFix"></image>
					</view>
				</view>
			</view>
		</view>
		<view class="wlInfo">
			<view class="line dFlex jBetween_aCenter" v-if="kddh">
				<view class="lineLeft">{{kdgs+"："+kddh}}</view>
				<view class="copy" @tap="copy(kddh)">复制</view>
			</view>
			<view class="address"><text>{{orders.source?'自提地址':'收货地址'}}</text>：{{orders.address_json.address+orders.address_json.address_detail}}</view>
		</view>
		<view class="content">
			<view class="log_list" v-if="datas.expressinfo.data.length">
				<view class='logisit' v-if="datas.expressinfo.data.length !=0" v-for="(item,index) in datas.expressinfo.data" :key='index'>
				  <view class='list' :style="{'border-left':index == datas.expressinfo.data.length-1 ? '':'1px solid #E5E5E5'}">
					<view class="left">
					  <image class='list_fin_img' v-if="index == 0&&datas.expressinfo.data.length>=1" src="../static/images/circle.png"></image>
					  <image class='list_start_img' v-if="index != 0" src="../static/images/gantan_icon3.png"></image>
					</view>
					<view class="right">
						<!-- <view class="tit" v-if="item.partTit">{{item.partTit}}</view> -->
						<view class="list_name" v-if="item.partTit" style="margin-bottom: 10px;">{{item.partTit}}</view>
					  <view :class="item.id == 0 ? 'list_name_last' : 'list_name' " >{{item.context}}</view>
					  <view :class="item.id == 0 ? 'list_dates_last' : 'list_dates' ">{{item.ftime}}</view>
					</view>
				  </view>
				</view>
			</view>
			<view class="empty imgPublic" v-else>
				<image src="https://kmbs-ytk.oss-cn-qingdao.aliyuncs.com/cMini/empty.png" mode="widthFix"></image>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		orderList,expressinfo
	} from '@/tools/http.js';
	import {domain} from '@/tools/config.js';
	import tools from '@/tools/tools.js';
	export default {
		data() {
			return {
				id:'',
				datas:{
					address:{},
					expressinfo:{
						data:[],
					}
				},
				orders:{
					ordergoods:[],
				},
				selGoodId:'',
				logList:[],
				kdgs:'',
				kddh:'',
				ordergoods:[],
			};
		},
		onLoad(e) {
			this.id = e.id;
		},
		onShow(){
			this.initData();
		},
		methods:{
			// 商品详情
			async initData(){
				let res = await orderList({
					ids:this.id
				});
				if(res){
					let line = res[0].ordergoods;
					this.selGoodId = line[0].id;
					this.orders = res[0];
					res[0].address_json = res[0].address_json?JSON.parse(res[0].address_json):{};
					if(!res[0].source){
						if(line[0].trade_status == 20 || line[0].trade_status == 60){
							this.kdgs = line[0].ship_type;
							this.kddh = line[0].ship_number;
							// 非拼团订单
							this.initWl(line[0]);
						}else if(line[0].trade_status == 10){//未发货
							this.datas.expressinfo.data = [];
							// 支付当日15点的时间戳
							let stopTime = new Date(this.orders.pay_time_text.split(' ')[0]+' 15:00:00').getTime() / 1000;
							let startTime = new Date(this.orders.pay_time_text.split(' ')[0]+' 09:00:00').getTime() / 1000;
							let time = new Date(this.orders.pay_time_text.split(' ')[0]+' 00:00:00').getTime() + 24 * 60 * 60 * 1000; 
							// let dealTime = stopTime>= this.orders.pay_time?this.orders.pay_time_text.split(' ')[0]+' 09:00':this.tool.timestampToTime(time).split(' ')[0]+' 09:00';
							let dealTime = '';
							if(this.orders.pay_time <= startTime){
								dealTime = this.orders.pay_time_text.split(' ')[0]+' 09:00';
							}else if(this.orders.pay_time > startTime && this.orders.pay_time <= stopTime){
								dealTime = tools.timestamptoData(time).split(' ')[0]+' 09:00';
							}
							if(line[0].storage_status == 1){//已出库
								this.datas.expressinfo.data.push({
									id:'1',
									partTit:'仓库处理中',
									context:'您的订单于'+dealTime+'开始打包中，请耐心等待。',
									ftime:tools.timestamptoData(line[0].sto_time * 1000),
								})
							}
							if(this.orders.source){//拼团
								this.datas.expressinfo.data.push({
									id:'1',
									partTit:'已下单',
									// context:'温馨提示：受疫情举措变化有影响，配送时效可能会有延长，我们蒋增加物流供应与人员安排，尽快为您送达，感谢您的理解与支持！',
									context:'您的订单正在处理中，请耐心等待。',
									ftime:this.orders.pay_time_text,
								})
							}else{//普通单品
								this.datas.expressinfo.data.push({
									id:'1',
									partTit:'已下单',
									context:'您的订单正在处理中，请耐心等待。',
									ftime:this.orders.pay_time_text,
								})
							}
							this.datas.expressinfo.data.push({
								id:'1',
								partTit:'',
								context:'您的订单预计'+dealTime+'开始处理，请您耐心等待',
								ftime:this.orders.pay_time_text,
							})
						}
					}else{
						// 拼团订单
						this.datas.address = res[0].address_json;
						this.datas.expressinfo.data = [];
						this.datas.expressinfo.data.push({
							id:'1',
							partTit:'已下单',
							// context:'温馨提示：您的订单预计'+res[0].ptTime+'送达，受疫情举措变化有影响，配送时效可能会有延长，我们将增加物流供应与人员安排，尽快为您送达，感谢您的理解与支持！',
							context:'您的订单正在处理中，请耐心等待。',
							ftime:line[0].pay_time_text,
						})
						let stopTime = new Date(this.orders.pay_time_text.split(' ')[0]+' 15:00:00').getTime() / 1000;
						let startTime = new Date(this.orders.pay_time_text.split(' ')[0]+' 09:00:00').getTime() / 1000;
						let time = new Date(this.orders.pay_time_text.split(' ')[0]+' 00:00:00').getTime() + 24 * 60 * 60 * 1000; 
						// let dealTime = stopTime>= this.orders.pay_time?this.orders.pay_time_text.split(' ')[0]+' 09:00':this.tool.timestampToTime(time).split(' ')[0]+' 09:00';
						let dealTime = '';
						if(this.orders.pay_time <= startTime){
							dealTime = this.orders.pay_time_text.split(' ')[0]+' 09:00';
						}else if(this.orders.pay_time > startTime && this.orders.pay_time <= stopTime){
							dealTime = tools.timestamptoData(time).split(' ')[0]+' 09:00';
						}
						this.datas.expressinfo.data.push({
							id:'1',
							partTit:'',
							context:'您的订单预计'+dealTime+'开始处理，请您耐心等待',
							ftime:this.orders.pay_time_text,
						})
						if(line[0].storage_status == 1){
							// 已出库
							this.datas.expressinfo.data.unshift({
								id:'1',
								partTit:'仓库处理中',
								context:'您的订单于'+dealTime+'开始打包中，请耐心等待',
								ftime:tools.timestamptoData(line[0].sto_time * 1000),
							})
							// 判断是否发货
							if(line[0].ps_status == 1){
								this.kdgs = line[0].ship_type;
								this.kddh = line[0].ship_number;
								// 已配送
								this.datas.expressinfo.data.unshift({
									id:'1',
									partTit:'运输中',
									context:'您的订单已于【'+tools.timestamptoData(line[0].sto_time * 1000)+'】司机取走，正在运输中，请耐心等待',
									ftime:tools.timestamptoData(line[0].ps_time * 1000),
								})
								if(res[0].status == 30 || res[0].status == 60){
									this.datas.expressinfo.data.unshift({
										id:'1',
										partTit:'已签收',
										// context:'您的快件已由自提点代收 请尽快联系团长提货     团长信息：'+res[0].address_json.receiver+' 手机号码：'+res[0].address_json.phone+' 提货地址：'+res[0].address_json.address+res[0].address_json.address_detail,
										context:'您的订单已经交于自提点【团长信息：'+res[0].address_json.receiver+' 手机号码：'+res[0].address_json.phone+' 提货地址：'+res[0].address_json.address+res[0].address_json.address_detail+'】请尽快联系团长提货,期待您的再次光临。',
										ftime:tools.timestamptoData(line[0].ps_time * 1000),
									})
								}
							}else if(line[0].ps_status == 2){
								// 配送异常
								this.datas.expressinfo.data.unshift({
									id:'1',
									partTit:'配送异常',
									context:line[0].ps_reason,
									ftime:'暂无',
								})
							}
						}else if(line[0].storage_status == 2){
							// 出库异常
							this.datas.expressinfo.data.unshift({
								id:'1',
								partTit:'出库异常',
								context:line[0].sto_reason,
								ftime:'暂无',
							})
						}
					}
					line.map((item,index)=>{
						let idx = this.tool.getIdx(this.ordergoods,'ship_number',item.ship_number);
						if(idx == '-1'){
							this.ordergoods.push(item);
						}
					})
				}
			},
			async initWl(goods){
				let res = await expressinfo({
					ogid:this.selGoodId
				});
				res = JSON.parse(res);
				if(res){
					res.address = JSON.parse(res.address);
					res.expressinfo = JSON.parse(res.expressinfo);
					res.expressinfo.data.map((item,index)=>{
						item.partTit='';
					})
					res.expressinfo.data.push({
						id:'1',
						partTit:'仓库处理中',
						context:'打包完成',
						ftime:tools.timestamptoData(goods.sto_time * 1000),
					})
					if(this.orders.source){//拼团
						res.expressinfo.data.push({
							id:'1',
							partTit:'已下单',
							// context:'温馨提示：您的订单预计'+goods.ptTime+'送达，受疫情举措变化有影响，配送时效可能会有延长，我们蒋增加物流供应与人员安排，尽快为您送达，感谢您的理解与支持！',
							context:'您的订单正在处理中，请耐心等待。',
							ftime:this.orders.pay_time_text,
						})
					}else{//普通单品
						res.expressinfo.data.push({
							id:'1',
							partTit:'已下单',
							context:'您的订单正在处理中，请耐心等待。',
							ftime:this.orders.pay_time_text,
						})
					}
					// 支付当日15点的时间戳
					let stopTime = new Date(this.orders.pay_time_text.split(' ')[0]+' 15:00:00').getTime() / 1000;
					let startTime = new Date(this.orders.pay_time_text.split(' ')[0]+' 09:00:00').getTime() / 1000;
					let time = new Date(this.orders.pay_time_text.split(' ')[0]+' 00:00:00').getTime() + 24 * 60 * 60 * 1000; 
					// let dealTime = stopTime>= this.orders.pay_time?this.orders.pay_time_text.split(' ')[0]+' 09:00':this.tool.timestampToTime(time).split(' ')[0]+' 09:00';
					let dealTime = '';
					if(this.orders.pay_time <= startTime){
						dealTime = this.orders.pay_time_text.split(' ')[0]+' 09:00';
					}else if(this.orders.pay_time > startTime && this.orders.pay_time <= stopTime){
						dealTime = tools.timestamptoData(time).split(' ')[0]+' 09:00';
					}
					res.expressinfo.data.push({
						id:'1',
						partTit:'',
						context:'您的订单预计'+dealTime+'开始处理，请您耐心等待',
						ftime:this.orders.pay_time_text,
					})
					this.datas = res;
				}
			},
			// 切换商品查看物流
			onchangeShop(line){
				this.datas.expressinfo.data = [];
				this.selGoodId = line.id;
				if(!this.orders.source){//非团购订单
					if(line.trade_status == 20 || line.trade_status == 60){//已发货
						this.kdgs = line.ship_type;
						this.kddh = line.ship_number;
						this.initWl(line);
					}else if(line.trade_status == 10){//未发货
						// 支付当日15点的时间戳
						let stopTime = new Date(this.orders.pay_time_text.split(' ')[0]+' 15:00:00').getTime() / 1000;
						let startTime = new Date(this.orders.pay_time_text.split(' ')[0]+' 09:00:00').getTime() / 1000;
						let time = new Date(this.orders.pay_time_text.split(' ')[0]+' 00:00:00').getTime() + 24 * 60 * 60 * 1000; 
						let dealTime = '';
						if(this.orders.pay_time <= startTime){
							dealTime = this.orders.pay_time_text.split(' ')[0]+' 09:00';
						}else if(this.orders.pay_time > startTime && this.orders.pay_time <= stopTime){
							dealTime = tools.timestamptoData(time).split(' ')[0]+' 09:00';
						}
						if(line.storage_status == 1){//已出库
							this.datas.expressinfo.data.push({
								id:'1',
								partTit:'仓库处理中',
								context:'打包完成',
								ftime:tools.timestamptoData(line.sto_time * 1000),
							})
						}
						if(this.orders.source){//拼团
							this.datas.expressinfo.data.push({
								id:'1',
								partTit:'已下单',
								context:'您的订单正在处理中，请耐心等待。',
								ftime:this.orders.pay_time_text,
							})
						}else{//普通单品
							this.datas.expressinfo.data.push({
								id:'1',
								partTit:'已下单',
								context:'您的订单正在处理中，请耐心等待。',
								ftime:this.orders.pay_time_text,
							})
						}
						
						this.datas.expressinfo.data.push({
							id:'1',
							partTit:'',
							context:'您的订单预计'+dealTime+'开始处理，请您耐心等待',
							ftime:this.orders.pay_time_text,
						})
					}
				}else{
					// 拼团订单
					this.datas.expressinfo.data.push({
						id:'1',
						partTit:'已下单',
						// context:'温馨提示：您的订单预计'+this.orders.ptTime+'送达，受疫情举措变化有影响，配送时效可能会有延长，我们蒋增加物流供应与人员安排，尽快为您送达，感谢您的理解与支持！',
						context:'您的订单正在处理中，请耐心等待。',
						ftime:line.pay_time_text,
					})
					let stopTime = new Date(this.orders.pay_time_text.split(' ')[0]+' 15:00:00').getTime() / 1000;
					let startTime = new Date(this.orders.pay_time_text.split(' ')[0]+' 09:00:00').getTime() / 1000;
					let time = new Date(this.orders.pay_time_text.split(' ')[0]+' 00:00:00').getTime() + 24 * 60 * 60 * 1000; 
					// let dealTime = stopTime>= this.orders.pay_time?this.orders.pay_time_text.split(' ')[0]+' 09:00':this.tool.timestampToTime(time).split(' ')[0]+' 09:00';
					let dealTime = '';
					if(this.orders.pay_time <= startTime){
						dealTime = this.orders.pay_time_text.split(' ')[0]+' 09:00';
					}else if(this.orders.pay_time > startTime && this.orders.pay_time <= stopTime){
						dealTime = tools.timestamptoData(time).split(' ')[0]+' 09:00';
					}
					this.datas.expressinfo.data.push({
						id:'1',
						partTit:'',
						context:'您的订单预计'+dealTime+'开始处理，请您耐心等待',
						ftime:this.orders.pay_time_text,
					})
					if(line.storage_status == 1){
						// 已出库
						this.datas.expressinfo.data.unshift({
							id:'1',
							partTit:'仓库处理中',
							context:'打包完成',
							ftime:tools.timestamptoData(line.sto_time * 1000),
						})
						// 判断是否发货
						if(line.ps_status == 1){
							this.kdgs = line.ship_type;
							this.kddh = line.ship_number;
							// 已配送
							this.datas.expressinfo.data.unshift({
								id:'1',
								partTit:'运输中',
								context:'您的订单已于【'+tools.timestamptoData(line.sto_time*1000)+'】司机取走，正在运输中，请耐心等待',
								ftime:tools.timestamptoData(line.ps_time * 1000),
							})
							if(this.orders.status == 30 || this.orders.status == 60){
								this.datas.expressinfo.data.unshift({
									id:'1',
									partTit:'已签收',
									// context:'您的快件已由自提点代收 请尽快联系团长提货     团长信息：'+this.orders.address_json.receiver+' 手机号码：'+this.orders.address_json.phone+' 提货地址：'+this.orders.address_json.address+this.orders.address_json.address_detail,
									context:'您的订单已经交于自提点【团长信息：'+this.orders.address_json.receiver+' 手机号码：'+this.orders.address_json.phone+' 提货地址：'+this.orders.address_json.address+this.orders.address_json.address_detail+'】请尽快联系团长提货,期待您的再次光临。',
									ftime:tools.timestamptoData(this.orders.ordergoods[0].ps_time * 1000),
								})
							}
						}else if(line.ps_status == 2){
							// 配送异常
							this.datas.expressinfo.data.unshift({
								id:'1',
								partTit:'配送异常',
								context:line.ps_reason,
								ftime:'暂无',
							})
						}
					}else if(line.storage_status == 2){
						// 出库异常
						this.datas.expressinfo.data.unshift({
							id:'1',
							partTit:'出库异常',
							context:line.sto_reason,
							ftime:'暂无',
						})
					}
				}
			},
			// 复制快递单号
			copy(number){
				uni.setClipboardData({
					data:number
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background:#f8f8f8;
	}
	.logistics{
		width: 100%;
		.pack{
			width: 100%;
			padding: 12px;
			box-sizing: border-box;
			background: white;
			margin-bottom: 12px;
			.packList{
				width:100%;
				margin-top:12px;
				overflow: hidden;
				overflow-x: auto;
				white-space: nowrap;
				.packOne{
					width: 35%;
					display: inline-block;
					margin-right: 12px;
					padding: 8px;
					box-sizing: border-box;
					border: 1px solid #E9E9E9;
					border-radius: 8px;
					.packInfo{
						display: inline-block;
						.oneTit{
							font-size: 16px;
						}
						.packCount{
							font-size: 12px;
							color: #999999;
							margin-top: 5px;
							text{
								display: inline-block;
								margin-bottom: 2px;
							}
						}
					}
					.packImg{
						display: inline-block;
						width:44px;
						height:44px;
						margin-left: 10px;
						float: right;
					}
				}
				.active{
					border-color: #FB5D02;
					.oneTit{
						color:#FB5D02;
						font-weight:bold;
					}
				}
				.packOne:last-child{
					margin: 0;
				}
			}
		}
		.content{
			width: 100%;
			padding: 12px;
			box-sizing: border-box;
			background: white;
			margin-top: 12px;
			// 物流追踪
			.log_list{
				width: 100%;
				height: 100%;
				margin-top: 10px;
				.logisit{
				  width: 100%;
				}
				.list{
					width: 90%;
					margin: auto;
					display: flex;
					justify-content: flex-start;
					position: relative;
					.left{
						margin-left: 10rpx;
						position: absolute;
						left: -26rpx;
						width: 30rpx;
						text-align: center;
						.list_fin_img{
							width: 30rpx;
							height: 30rpx;
						}
						.list_start_img{
						   width: 24rpx;
						   height: 24rpx;
						}
					}
					.right{
						width: 90%;
						margin-left: 50rpx;
						margin-bottom: 50rpx;
						.tit{
							font-weight: bold;
							margin-bottom: 10px;
							font-size: 16px;
						}
						.list_name_last, .list_name{
						  width: 100%;
						  margin-top: -15rpx;
						  text-align: left;
						}
						.list_name_last{
							color: #00C050;
						}
						.list_name{
						  color: #999999;
						}
						.list_dates_last{
						  color: #00C050;
						  margin-top: 10rpx;
						}
						.list_dates{
						  margin-top: 10rpx;
						  color: #999999;
						}
					}
				}
				
			}
		}
		.wlInfo{
			width: 100%;
			padding: 12px;
			box-sizing: border-box;
			background: white;
			.line{
				margin-bottom: 12px;
				.lineLeft{
					flex: 1;
				}
			}
			.copy{
				font-size: 12px;
				color: #666666;
			}
			.address{
				color: #999999;
			}
		}
	}
</style>
